//
//  LCSettingVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/1.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCSettingVC.h"
#import "LCSettingCell.h"
#import "LCSettingFooter.h"

@interface LCSettingVC ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate>


@property (nonatomic, strong) NSArray *datasArr;
@end

@implementation LCSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _datasArr = @[
                  @[@"账号设置"],
                  @[@"云添加设置"],
                  @[@"允许移动网络上传下载",@"消息通知"],
                  @[@"清楚缓存"]
                  ];
}

#pragma mark - -
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
    
        LCSettingFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"settingFooter" forIndexPath:indexPath];
        footer.logout = ^{
            [self logout];
        };
        return footer;
    }
    return nil;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;

    
    LCSettingCell *settingCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"appSettingCell" forIndexPath:indexPath];
    if (self.datasArr.count > section) {
        NSArray *itemData = self.datasArr[section];
        if (itemData.count > row) {
            [settingCell lc_updateSettingCellWithParameter:itemData[row]];
        }
    }
    switch (section) {
        case 0:  //  账号设置
        case 1:  //  云添加设置
        {
            settingCell.arrowImage.hidden = NO;
            
        }
            break;
        case 2:  //  允许移动网络下载  消息通知
        {
            settingCell.switchAction.hidden = NO;
        }
            break;
        case 3:  //  清楚缓存
        {
            
        }
            break;
            
        default:
            break;
    }
    return settingCell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _datasArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSArray *itemCountArr = self.datasArr[section];
    return itemCountArr.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    
    CGFloat footerHeight =  (section == (_datasArr.count - 1)) ? 70 : 0;
    return CGSizeMake(SWidth, footerHeight);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SWidth, 50);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;

    switch (section) {
        case 0:  //  账号设置
        {
            [self performSegueWithIdentifier:@"showAccountSetting" sender:self];
        }
            break;
        case 1:  //  云添加设置
        {
            
        }
            break;
        case 2:  //  允许移动网络  消息通知
        {
            
        }
            break;
        case 3:  //  清除缓存 关于我们
        {
            switch (row) {
                case 0:  /*  清除缓存  */
                {
                    [self cleanCacheTip];
                }
                    break;
                    
                default:
                    break;
            }
            
        }
            break;
        default:
            break;
    }
}

- (void)cleanCacheTip {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否确认清楚缓存" preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (void)logout {
    
    

    [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/logout" parameter:@{} responseSuccess:^(id resultCode, id resultParameter) {
        
        if ([resultCode isEqualToString:@"0"]) {
            
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"token"];
            [[NSUserDefaults standardUserDefaults]synchronize];
            UIViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LCLoginNavigationVC"];
            [UIApplication sharedApplication].keyWindow.rootViewController = loginVC;

        }

        LCLog(@"%@",resultParameter);
    } responseFail:^(id resultCode, NSError *error) {
        
    }];
}
- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
